/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.OnlineHouseLeaseSystem.database;

import java.sql.Statement;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.ResultSet;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 *
 * @author gas1121
 */
public class DBManager {
    private Connection connection;
    private Statement statement;
    private ResultSet resultset;

    public void init()
    {
        try
        {
            DriverManager.registerDriver( (Driver)
            Class.forName(
            		"org.logicalcobwebs.proxool.ProxoolDriver").newInstance()
            );
            connection = DriverManager.getConnection(
                    "proxool.OnlineHouseLeaseSystem");
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public Connection getConnection()
    {
        if (connection == null)
        {
            init();
        }
        return connection;
    }

    public int executeUpdate(String sql) throws SQLException
    {
        statement = getConnection().createStatement();
        return statement.executeUpdate(sql);
    }

    public ResultSet executeQuery(String sql) throws SQLException
    {
        statement = getConnection().createStatement();
        return statement.executeQuery(sql);
    }

    public void close()
    {
        if(resultset != null)
        {
            try
            {
                resultset.close();
            }
            catch (SQLException e) {}
        }
        if(statement != null)
        {
            try
            {
                statement.close();
            }
            catch (SQLException e) {}
        }
        if(connection != null)
        {
            try
            {
                connection.close();
            }
            catch (SQLException e) {}
        }
    }
}
